﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.OleDb;
using System.Threading;
using ToolUtil;
using zyconfig;
using zymarc;

namespace zymain
{
    public partial class InserBookWork :  BackgroundWorker
    {
        public string Marc;
        protected Thread HashThread;
        public List<int> jms;
        public int num;
        public InserBookWork()
        {
            InitializeComponent();
        }

        protected override void OnDoWork(DoWorkEventArgs e)
        {
            using (DbOperHandler _open = new Common().Doh())
            {
                MarcConvert convert = new MarcConvert();
                convert.MarcText = Marc;
                string isbn = convert.GetASubField("010", "a", 1);
                _open.Reset();
                if (!string.IsNullOrEmpty(isbn))
                {
                    string sisbn = StringUtil.BuingISBNSept(StringUtil.BarCodeToISBN(isbn.Replace("-", "")));
                    _open.AddFieldItem("标准编码", isbn);
                }
                string ydj = convert.GetASubField("010", "d", 1);
                double dj = StringUtil.GetDj(ydj);
                if (dj == 0.00)
                {
                    dj = 5.00;
                    convert.ReplaceSubfield("010", "d", 1, "CNY" + dj.ToString());
                }
                string title = convert.GetASubField("200", "a", 1);
                if (!string.IsNullOrEmpty(title))
                {
                    title = StringUtil.ToDBC(title).Replace(@"'", "\'");
                    _open.AddFieldItem("主键码", jms[0]);
                    string press = convert.GetASubField("210", "c", 1);
                    if (!string.IsNullOrEmpty(press))
                    {
                        press = StringUtil.ToDBC(press).Replace(@"'", "\'");
                        _open.AddFieldItem("出版者", press);
                    }
                    string pdate = convert.GetASubField("210", "d", 1);
                    if (!string.IsNullOrEmpty(pdate))
                    {
                        pdate = StringUtil.ToDBC(pdate).Replace(@"'", "\'");
                        _open.AddFieldItem("出版日期", pdate);
                    }
                    else
                    {
                        pdate = DateTime.Now.Year.ToString();
                    }
                    string author = convert.GetASubField("200", "f", 1);
                    if (!string.IsNullOrEmpty(author))
                    {
                        author = StringUtil.ToDBC(author).Replace(@"'", "\'");
                        _open.AddFieldItem("责任者", author);
                    }
                    string zrjg = convert.GetASubField("701", "p", 1);
                    if (!string.IsNullOrEmpty(zrjg))
                    {
                        zrjg = StringUtil.ToDBC(zrjg).Replace(@"'", "\'");
                        _open.AddFieldItem("责任者单位", zrjg);
                    }
                    string xgh = convert.GetASubField("701", "d", 1);
                    if (!string.IsNullOrEmpty(xgh))
                    {
                        xgh = StringUtil.ToDBC(xgh).Replace(@"'", "\'").Replace("?","");
                        _open.AddFieldItem("学工号", xgh);
                    }
                    _open.AddFieldItem("题名", title);
                    _open.AddFieldItem("库键码", jms[2]);
                    _open.AddFieldItem("处理日期", DateTool.stringToDate(pdate + "-09-20", DateTool.formatDate2));
                    _open.AddFieldItem("marc", Marc.Replace(@"'", "\'"));
                    try
                    {
                        _open.Insert("馆藏书目库");
                        //Console.WriteLine("新增馆藏ID:"+zjm + ":::" + czjm);
                        _open.Reset();
                        _open.SqlCmd = "SET IDENTITY_INSERT 采购库 ON ";
                        _open.ExecuteSqlNonQuery();
                        _open.Reset();
                        if (!string.IsNullOrEmpty(xgh))
                        {
                            _open.AddFieldItem("登录号", xgh);
                            _open.AddFieldItem("册数", 1);
                            _open.AddFieldItem("金额", dj);
                            _open.AddFieldItem("状态", "G");
                            _open.AddFieldItem("入藏日期", DateTool.stringToDate(pdate + "-09-20", DateTool.formatDate2));
                        }
                        else
                        {
                            _open.AddFieldItem("状态", "C");
                        }
                        _open.AddFieldItem("子键码", jms[1]);
                        _open.AddFieldItem("主键码", jms[0]);
                        _open.AddFieldItem("库键码", jms[2]);
                        _open.AddFieldItem("发行商键码", 1);
                        _open.AddFieldItem("经费来源键码", 0);
                        _open.AddFieldItem("单价", dj);
                        _open.AddFieldItem("币种", "CNY");
                        _open.AddFieldItem("馆键码", 1);
                        _open.Insert("采购库");
                        _open.Reset();
                        _open.SqlCmd = "SET IDENTITY_INSERT 采购库 OFF ";
                        _open.ExecuteSqlNonQuery();
                        if (!string.IsNullOrEmpty(xgh))
                        {
                            try
                            {
                                _open.Reset();
                                _open.AddFieldItem("子键码", jms[1]);
                                _open.AddFieldItem("主键码", jms[0]);
                                _open.AddFieldItem("库键码", jms[2]);
                                _open.AddFieldItem("馆键码", 1);
                                _open.AddFieldItem("登录号", xgh);
                                _open.AddFieldItem("条形码", xgh);
                                _open.AddFieldItem("馆藏地址", "B");
                                _open.AddFieldItem("虚拟库室", 2);
                                _open.AddFieldItem("操作员", 1);
                                _open.AddFieldItem("状态", "G");
                                _open.AddFieldItem("刊价", dj);
                                string ssh = convert.GetASubField("690", "a", 1);
                                if (!string.IsNullOrEmpty(ssh))
                                    _open.AddFieldItem("索书号", ssh);
                                _open.AddFieldItem("处理日期", DateTool.stringToDate(pdate + "-09-20", DateTool.formatDate2));
                                _open.Insert("馆藏典藏库");
                            }
                            catch (OleDbException ex)
                            {
                                //Console.WriteLine(xgh + ":" + ex.Message);
                                LogManager.WriteLog("典藏出错", xgh + ":" + ex.Message);
                            }
                        }
                    }
                    catch (OleDbException ex)
                    {
                        Console.WriteLine(ex.Message);
                        LogManager.WriteLog("导入馆藏出错",  ex.Message);
                        num = 0;
                    }
                }
            }
            base.OnDoWork(e);
        }
        protected override void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
        {
            if ((this.HashThread != null) && this.HashThread.IsAlive)
            {
                this.HashThread.Abort();
            }
            base.OnRunWorkerCompleted(e);
        }

        public void RunWorkerSync(DoWorkEventArgs e)
        {
            this.OnDoWork(e);
            base.OnRunWorkerCompleted(new RunWorkerCompletedEventArgs(e.Result, null, false));
        }
    }
}
